package org.example.zuoye.api;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.zuoye.entity.Admin;
import org.example.zuoye.entity.Appoint;
import org.example.zuoye.entity.Result;
import org.example.zuoye.service.AppointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author xinchen
 * @Date: 2024/08/15/ 11:48
 * @Description 预约表
 */
@RestController
@RequestMapping(value = "/api/v1/appoint")
@Slf4j
public class AppointApi {
    @Autowired
    private AppointService appointService;

    /**
     * 条件 + 分页查询
     */
    @GetMapping
    public Result getAll(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit, Appoint appoint) {
        PageInfo pi = appointService.search(page, limit, appoint);
        return Result.success(pi);
    }
    /**
     * 查询sid
     */
    @GetMapping("/sid/{sid}")
    public Result getBySid(@PathVariable Integer sid) {
        try {
            List<Appoint> appoint = appointService.getBySid(sid);
            return Result.success(appoint);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    /**
     * add
     */
    @PostMapping
    public Result add(@RequestBody Appoint appoint) {
        try {
            appointService.add(appoint);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 取消预约
     */
    @GetMapping("/id/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            boolean successs = appointService.updateById(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增预约
     */
    @DeleteMapping
    public Result remove(@RequestBody Integer[] ids){
        try {
            appointService.editById(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    /**
     * update
     */
    @PutMapping
    public Result edit(@RequestBody Appoint appoint){
        try {
            appointService.edit(appoint);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    // 删除操作
    @DeleteMapping("/id/{id}")
    public Result remove(@PathVariable Integer id){
        try {
            int count = appointService.removeById(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改状态:已完成
     */
    @PutMapping("/status")
    public Result updateByStatue(@RequestBody Appoint appoint){
        try {
           int count = appointService.updateByStatue(appoint);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 给用户添加病例
     */
    @PutMapping("/remark")
    public Result updateByRemark(@RequestBody Appoint appoint){
        try {
            int count = appointService.updateByRemark(appoint);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
